package licode.unisop.log.service.adapter.util;

import cn.hutool.core.util.StrUtil;
import licode.unisop.log.client.api.AuditLogApi;
import licode.unisop.log.client.api.AuditLogRecord;
import licode.unisop.log.client.dto.AuditLogDTO;
import licode.unisop.platform.client.api.SiPlatformAuthService;
import licode.unisop.platform.client.vo.PlatAuthVO;
import licode.unisop.provider.info.SiPlatIdFind;
import licode.unisop.provider.info.UserIdInfo;
import licode.unisop.provider.vo.SiUser;
import licode.unisop.realm.client.api.SiUserQueryService;
import licode.unisop.types.ActionStatus;
import licode.unisop.types.ActionType;
import licode.unisop.types.dp.UserAuthIn;
import licode.unisop.types.util.AuStrUtil;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 封装审计日志调用
 *
 * @author WR
 * @since 2016/7/16 9:29
 */
@Primary
@Component
public class AuditLogRecordImpl implements AuditLogRecord {
    @Resource
    private AuditLogApi auditLogApi;

    @Resource
    private SiPlatformAuthService siPlatformAuthService;

    @Resource
    private SiUserQueryService siUserQueryService;

    private AuditLogDTO buildAuditLog(SiUser userInfo, PlatAuthVO platform) {
        AuditLogDTO auditLogDto = new AuditLogDTO();

        auditLogDto.setPlatformId(null != platform ? platform.getPlatformId() : "");
        auditLogDto.setPlatformName(null != platform ? platform.getName() : "");
        auditLogDto.setUserId(userInfo != null ? userInfo.getId() : "");

        if (null != userInfo) {
            if (StrUtil.isNotBlank(userInfo.getNickName())) {
                auditLogDto.setUserName(userInfo.getNickName());
            } else if (StrUtil.isNotBlank(userInfo.getName())) {
                auditLogDto.setUserName(userInfo.getName());
            } else {
                auditLogDto.setUserName("");
            }
        } else {
            auditLogDto.setUserName("");
        }

        auditLogDto.setActionTime(LocalDateTime.now());
        auditLogDto.setActionError("");
        auditLogDto.setActionData("");

        return auditLogDto;
    }

    @Override
    public void log(UserAuthIn authToken, String type, String data, String err) {
        SiUser userInfo = siUserQueryService.lookupUserById(UserIdInfo.builder()
                .realmId(authToken.getRealmId())
                .userId(authToken.getUserId())
                .build());
        PlatAuthVO platform = siPlatformAuthService.fromId(SiPlatIdFind.builder()
                .platformId(authToken.getPlatformId()).build());
        AuditLogDTO auditLogDto = buildAuditLog(userInfo, platform);

        auditLogDto.setClientId(authToken.getClientId() == null ? "" :
                authToken.getClientId());
        auditLogDto.setTerminalId(authToken.getTerminalId() == null ? "" :
                authToken.getTerminalId());

        auditLogDto.setModuleName("认证系统");
        auditLogDto.setUserAccount(authToken.getAccount());
        auditLogDto.setUserRole(authToken.getRoleNames());

        auditLogDto.setActionType(type);
        auditLogDto.setActionData(AuStrUtil.getContent(data, ActionType.MAX_DATA));

        if (StrUtil.isBlank(auditLogDto.getPlatformId())) {
            auditLogDto.setPlatformId(null == authToken.getPlatformId() ? "" :authToken.getPlatformId());
        }

        if (null == auditLogDto.getTenantId()) {
            auditLogDto.setTenantId("");
        }

        if (null == auditLogDto.getTerminalId()) {
            auditLogDto.setTerminalId("");
        }

        auditLogDto.setActionStatus(err.isEmpty() ? ActionStatus.OK : ActionStatus.FAIL);
        auditLogDto.setActionError(err);

        auditLogApi.save(auditLogDto);
    }
}
